package com.zhm.demo.modbus;

import com.ghgande.j2mod.modbus.Modbus;
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
import com.ghgande.j2mod.modbus.procimg.InputRegister;
import com.ghgande.j2mod.modbus.procimg.Register;
import com.ghgande.j2mod.modbus.procimg.SimpleRegister;
import com.ghgande.j2mod.modbus.util.BitVector;

import java.net.InetAddress;

public class ModbusTCPClient {
    public static void main(String[] args) {
        try {
            // 设置目标 IP 和端口
            int port = Modbus.DEFAULT_PORT;

            // 创建 ModbusTCPMaster 实例
            ModbusTCPMaster master = new ModbusTCPMaster("127.0.0.1", port);
            master.connect();

            // 读取保持寄存器（假设从站地址为 1，寄存器地址从 0 开始，读取 10 个寄存器）

            InputRegister[] registers1 = master.readInputRegisters(1, 200, 1);

            // 打印读取到的寄存器值
            for (int i = 0; i < registers1.length; i++) {
                System.out.println("Holding Register1 " + (i) + " = " + registers1[i].getValue());
            }


            BitVector registers2 = master.readInputDiscretes(1, 601, 8);

            // 打印读取到的寄存器值

            System.out.println("Holding Register2 " + " = " + registers2.getBit(0));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(1));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(2));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(3));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(4));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(5));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(6));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(7));


            BitVector registers3 = master.readCoils(1, 11, 8);
            System.out.println("Holding Register3 " + " = " + registers3.getBit(0));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(1));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(2));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(3));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(4));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(5));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(6));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(7));
            master.writeCoil(1,11,false);
            registers3 = master.readCoils(1, 11, 1);
            System.out.println("Holding Register3 " + " = " + registers3.getBit(0));

            Register[] registers = master.readMultipleRegisters(1, 800, 2);
            for (Register register : registers) {
                System.out.println("Holding Register4 " + " = " + register.getValue());
            }
            master.writeMultipleRegisters(1, 800, new Register[]{new SimpleRegister(100), new SimpleRegister(200)});
            registers = master.readMultipleRegisters(1, 800, 2);
            for (Register register : registers) {
                System.out.println("Holding Register4 " + " = " + register.getValue());
            }
            // 断开连接
            master.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}